package com.sinocare.project.report.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinocare.project.common.entity.SccReportList;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.report.service.SccReportListService;
import com.sinocare.project.report.vo.SccMedicalResultVo;
import com.sinocare.project.report.vo.SccReportListVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 检测项结果列表(SccReportList)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:53:21
 */
@RestController
@RequestMapping("sccReportList")
@CrossOrigin
public class SccReportListController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccReportListService sccReportListService;

    /**
     * 分页查询所有数据
     *
     * @param data 查询实体
     * @return 所有数据
     */
    @PostMapping(value = "test")
    public Result selectAll(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)){
            SccReportListVo vo = JSON.parseObject(data, SccReportListVo.class);
            wrapper.eq("c.medical_type",vo.getGlu());
            wrapper.ge("b.test_time",vo.getStart());
            wrapper.le("b.test_time",vo.getEnd());
            wrapper.eq(vo.getSex()!=null,"a.sex",vo.getSex());
            wrapper.eq(!StrUtil.hasBlank(vo.getMainStore()),"d.parent_code",vo.getMainStore());
            wrapper.eq(!StrUtil.hasBlank(vo.getSubStore()),"d.store_code",vo.getSubStore());
            if (!StrUtil.hasBlank(vo.getAge())){
                String[] split = vo.getAge().split("-");
                wrapper.ge("a.age",split[0]);
                wrapper.le("a.age",split[1]);
            }
        }
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.getSexTestTime(wrapper));
    }

    @PostMapping(value = "hgl")
    public Result selectHgl(String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)){
            SccMedicalResultVo vo = JSON.parseObject(data, SccMedicalResultVo.class);
            wrapper.ge("b.test_time",vo.getStart());
            wrapper.le("b.test_time",vo.getEnd());
            wrapper.eq(!StrUtil.hasBlank(vo.getHeadStore()),"d.parent_code",vo.getHeadStore());
            wrapper.eq(!StrUtil.hasBlank(vo.getSubStore()),"d.store_code",vo.getSubStore());
        }
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.getHgl(wrapper));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sccReportList 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccReportList sccReportList) {
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.save(sccReportList));
    }

    /**
     * 修改数据
     *
     * @param sccReportList 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccReportList sccReportList) {
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.updateById(sccReportList));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS,this.sccReportListService.removeByIds(idList));
    }
}

